package com.mth480.rebasedownloader;

import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import org.jdom.Element;
import com.biomatters.geneious.publicapi.plugin.*;
import jebl.util.ProgressListener;
/**
 * 
 * @author Nick LaVigne, Jake Almer
 * @version 0.1
 */
public class RebaseDownloaderPlugin extends GeneiousPlugin {
    public String getName() {
        return "Rebase Downloader!";
    }

    public String getHelp() {
        return "Download restriction enzyme sequences" +
        		"from the REBASE website";
    }

    public String getDescription() {
        return "Rebase Downloader plugin";
    }

    public String getAuthors() {
        return "Nick LaVigne, Jake Almer";
    }

    public String getVersion() {
        return "0.1";
    }

    public String getMinimumApiVersion() {
        return "3.0";
    }

    public int getMaximumApiVersion() {
        return 3;
    }
    
    public GeneiousService[] getServices()
    {
    	changeClassPath();
    	RebaseFields.init();
    	GeneiousService[] services = new GeneiousService[1];
    	services[0] = new RebaseDBService();
    	return services;
    }
    
    private void changeClassPath() {
//    	in order for this to work, we have to manually add the RestrictionEnzymeDate class
		Class[] parameters = new Class[]{URL.class};
		//File f = new File(System.getProperty("user.dir") + "/plugins/com.mth480.rebasedownloader.RebaseDownloaderPlugin/com.mth480.rebasedownloader.jar");
		File f = new File("/home/lavignni/bin/Geneious/plugins/com.mth480.rebasedownloader.RebaseDownloaderPlugin/com.mth480.rebasedownloader.jar");
		//System.out.println("+++++++++++++++++++++++++++++"+f.toString());
		URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
		Class sysclass = URLClassLoader.class;
	 
		try {
			URL u = f.toURL();
			Method method = sysclass.getDeclaredMethod("addURL",parameters);
			method.setAccessible(true);
			method.invoke(sysloader,new Object[]{ u });
		} catch (Throwable t) {
			t.printStackTrace();
		}//end try catch
    }
    
    
    public DocumentType[] getDocumentTypes() {
        return new DocumentType[]{new DocumentType("Rebase Xmls", RestrictionEnzyme.class, null)};
    }

    public DocumentFileImporter[] getDocumentFileImporters() {
        return new DocumentFileImporter[]{new RebaseDocumentImporter()};
    }

    class RebaseDocumentImporter extends DocumentFileImporter {

        public String[] getPermissibleExtensions() {
            return new String[]{".rxml"};
        }

        public String getFileTypeDescription() {
            return "Rebase Xml";
        }

        public final AutoDetectStatus tentativeAutoDetect(File file, String fileContentsStart) {
            return AutoDetectStatus.ACCEPT_FILE;
        }

        public void importDocuments(File file, ImportCallback importCallback, ProgressListener progressListener) throws IOException, DocumentImportException {
            importCallback.add(new RestrictionEnzyme(file));
         }
    }
}
